<?php
declare (strict_types=1);

namespace app\api\controller;

use app\BaseController;
use app\common\model\WebConfig;
use app\common\service\ImageHashService;
use think\facade\Cache;
use think\facade\Db;

class Index extends BaseController
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 75px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) API应用层</h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * 答题规则和考试说明
     */
    public function getExamDesc()
    {
        $config = WebConfig::getItem('web');
        $data = [
            'site_rule' => $config['site_rule'],
            'exam_explain' => $config['exam_explain'],
        ];
        return message("操作成功", true, $data);
    }

    /**
     * @return array
     * 报名提交接口
     */
    public function SubSignUp()
    {
        $params = input('post.');
        $rule = [
            'name|姓名' => 'require',
            'idcard|身份证' => 'require',
            'examinee_number|考生报名号' => 'require|min:12|max:12',
            'end_school|初中学校名称' => 'require',
            'school_address|学校所在区县' => 'require',
            'address|家庭地址' => 'require',
            'father_mobile|联系人手机号1' => 'require|mobile',
            'mother_mobile|联系人手机号2' => 'require|mobile',
//            'volunteer|报考志愿' => 'require',
            'source|考生类型' => 'require',
            'major|意向报考专业' => 'require',
//            'hobby|其他专业方向' => 'require',
//            'file_pdf|PDF文件未上传' => 'require',
        ];
        $validate = \think\facade\Validate::rule($rule);
        if (!$validate->check($params)) {
            return message($validate->getError(), false);
        }
        // if(time()>1716337800){
        //     return message('报名通道已关闭', false);
        // }
        $isidcard = is_idcard($params['idcard']);
        if ($isidcard == false) {
            return message('您的身份证信息校验失败', false);
        }
        $isexist = Db::name('signup')
            ->where('examinee_number', $params['examinee_number'])
            ->where('mark', 1)
            ->value('id');
        if ($isexist) {
            return message('该考生报名号已报名成功', false);
        }
        $isexistidcard = Db::name('signup')
//            ->where('idcard', $params['idcard'])
            ->where('idcard', $params['idcard'])
            ->where('mark', 1)
            ->value('id');
        if ($isexistidcard) {
            return message('该身份证号已报名成功', false);
        }
        $idcardInfo = calculateAgeAndGender($params['idcard']);
        $data = [
            'name' => $params['name'],
            'idcard' => $params['idcard'],
            'examinee_number' => $params['examinee_number'],
            'end_school' => $params['end_school'],
            'school_address' => $params['school_address'],
            'address' => $params['address'],
            'father_mobile' => $params['father_mobile'],
            'mother_mobile' => $params['mother_mobile'],
//            'volunteer' => $params['volunteer'],
//            'hobby' => $params['hobby'],
            'major' => $params['major'],
            'gender' => $idcardInfo['gender'],
            'age' => $idcardInfo['age'],
            'birthday' => $idcardInfo['birthday'],
            'source' => $params['source'],
            'signtime' => date('Y-m-d H:i:s'),
            'status' => 1,
            'create_time' => time(),
            'file_pdf' => isset($params['file_pdf']) ? $params['file_pdf'] : '',
        ];
        Db::name('signup')->insert($data);
        return message('报名成功', true);
    }

    /**
     * @return array
     * 上传文件
     */
    public function upImage()
    {
        // 错误提示语
        $error = "";
        // 上传图片
        $ImageHashService = new ImageHashService();
        $result = $ImageHashService->upload_image('file', '', $error);
        if (!$result) {
            return message($error, false);
        }
        $baseUrl = $this->getBaseUrl();
        // 结果处理
        $list = [];
        foreach ($result as $key => $value) {
            if (is_array($value)) {
                // 二维数组(多图上传)
                foreach ($result as $val) {
                    $list[] = $baseUrl . $val['filepath'];
                }
            } else {
                // 一维数组(单图上传)
                $list = $baseUrl . $result['filepath'];
            }
        }
        return message("上传成功", true, $list);
    }

    /**
     * @return mixed|string
     * 返回baseurl
     */
    private function getBaseUrl()
    {
        $configData = Cache::get('storage');
        $configData = json_decode($configData, true);
        switch ($configData['name']) {
            case 'qiniu':
                $baseUrl = $configData['config']['url'];
                break;
            case 'aliyun':
                $baseUrl = $configData['config']['url'];
                break;
            case 'qcloud':
                $baseUrl = $configData['config']['scheme'] . '://' . $configData['cdn'];
                break;
        }
        return $baseUrl;

    }

    /**
     * 查询报名信息
     */
    public function queryInfo()
    {
        $params = input('post.');
        $rule = [
            'idcard|身份证' => 'require',
            'examinee_number|考生报名号' => 'require|min:12|max:12',
        ];
        $validate = \think\facade\Validate::rule($rule);
        if (!$validate->check($params)) {
            return message($validate->getError(), false);
        }
        $map = [
            'idcard' => $params['idcard'],
            'examinee_number' => $params['examinee_number'],
        ];
        $info = Db::name('signup')
            ->where($map)
            ->find();
        if(empty($info)){
            return message("查询信息", true);
        }
        return message("查询信息", true, $info);
    }
}
